import { configureStore } from '@reduxjs/toolkit'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // 默认是 localStorage
import rootReducer from './reducers/index'

// 持久化配置
const persistConfig = {
  key: 'index',        // 存储的键名
  storage,           // 存储方式
  whitelist: ['basic', 'common'],  // 需要持久化的reducer
}

// 包装 reducer
const persistedReducer = persistReducer(persistConfig, rootReducer)


// 创建 store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
      getDefaultMiddleware({
          serializableCheck: {
              // 忽略 redux-persist 的动作类型
              ignoredActions: ['persist/PERSIST']
          }
      })
})

// 创建持久化 store
export const persistor = persistStore(store)
